

#![warn(clippy::all, clippy::pedantic)] // enable all clippy lints
pub mod codegen;
pub mod lexer;
pub mod parser;
pub mod vm;


pub fn compile_and_run(vm: &mut vm::VM, src: &  str) {
      let  ast  = parser::parse(src); 
      println!("{:#?}", ast); 
    let instructions = codegen::compile(ast );
    vm.run(instructions);

    for i in 0u8..26 {
        if vm.globals[i as usize] != 0 {
            println!("{} =  {}", (i + 97) as char, vm.globals[i as usize]);
        }
    }
}
fn main() {
    use std::io::BufRead;
    let mut vm = vm::VM::new();
    // 读取文件内容, 逐行编译并运行

    let file = std::fs::File::open("tinyC.txt").unwrap();
    let reader = std::io::BufReader::new(file);
    // for line in reader.lines() {
    //     let     line = line.unwrap(); // 解包会移动变量, 变更所有权, 所以只能解包一次
    //     println!("Compiling and running: {}", &line);
    //       compile_and_run(&mut vm, &  line );
    // }
    for line in std::io::stdin().lock().lines() {
        let     line = line.unwrap(); // 解包会移动变量, 变更所有权, 所以只能解包一次
        println!("Compiling and running: {}", &line);
         compile_and_run(&mut vm, &line);
    }
}